package com.wis.compress;

import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class CompressionDouglas {
    /**
     * 数据压缩算法
     * @param array coordinates点数据集
     * @return
     */
    public static JSONArray compression(JSONArray array){
        List<Point> points = new ArrayList();
        JSONArray pointsArray = new JSONArray();
        for(int i=0;i<array.size();i++){
            if (array.get(i) instanceof JSONArray){
                JSONArray o = array.getJSONArray(i);
                Point point = new Point();
                point.setX(o.getDouble(0));
                point.setY(o.getDouble(1));
                points.add(point);
            }
        }
        Douglas douglas = new Douglas(points);
        douglas.compress(douglas.points.get(0),douglas.points.get(douglas.points.size()-1));
        for(int j = 0;j<douglas.points.size();j++){
            Point p = douglas.points.get(j);
            if (p.getIndex()>-1){
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(p.getX());
                jsonArray.add(p.getY());
                pointsArray.add(jsonArray);
            }
        }
        return pointsArray;
    }
}
